package com.neusoft.controller;

import com.neusoft.base.BaseController;
import com.neusoft.model.CategorySearch;
import com.neusoft.po.Category;
import com.neusoft.service.CategoryService;
import com.neusoft.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 商品分类请求控制器
 *
 * @author Lucifer
 * @since 2021/5/16 11:27
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类接口")
public class CategoryController extends BaseController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private ItemService itemService;

    @GetMapping("/all")
    @ApiOperation(value = "消费用户首页看到的所有商品", notes = "消费用户进入首页需要看到所有商品")
    public List<Category> all() {
        return categoryService.all();
    }

    @PostMapping("/page")
    @ApiOperation(value = "管理用户看到的分类数据", notes = "管理用户看到的分类数据")
    public Object page(@RequestBody CategorySearch pageSearch) {
        return categoryService.page(pageSearch);
    }

    @GetMapping
    @ApiOperation("获取商品分类")
    @ApiImplicitParam(value = "商品分类ID", name = "id", required = true)
    public Category get(Integer id) {
        return categoryService.getById(id);
    }

    @GetMapping("/byItemId")
    @ApiOperation("根据商品ID查询分类")
    @ApiImplicitParam(value = "商品ID", name = "item_id", required = true)
    public Category getByItemId(Integer item_id) {
        return categoryService.getById(itemService.getById(item_id).getCategory_id());
    }

    @PostMapping
    @ApiOperation("保存分类")
    public Integer save(@RequestBody Category category) {
        categoryService.save(category);
        return category.getId();
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("单个商品分类删除（新增，修改同）")
    public Integer delete(@PathVariable("ids") Integer[] ids) {
        return categoryService.delete(ids);
    }

    @GetMapping("/check")
    @ApiOperation("检查分类下有无商品")
    @ApiImplicitParam(value = "商品分类ID集合JSON", name = "ids", required = true)
    public void check(Integer[] ids) {
        categoryService.check(ids);
    }

}
